package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.Enum.EnumRelevanceStatus;
import com.mf.Enum.EnumWorkType;
import com.mf.entity.dto.DtoIncoming;
import com.mf.entity.dto.DtoIncomingDetail;
import com.mf.entity.req.ReqRelevancyById;
import com.mf.entity.req.publicReq.ReqPublicIds;
import com.mf.exception.GraceException;
import com.mf.service.ServiceIncoming;
import com.mf.service.ServiceIncomingDetail;
import com.mf.service.ServiceSort;
import com.mf.utils.UtilConverter;
import com.mf.utils.UtilOperatorWorkType;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import com.mf.entity.dto.DtoSortDetail;
import com.mf.entity.req.search.SearchReqSortDetail;
import com.mf.mapper.MapperSortDetail;
import com.mf.service.ServiceSortDetail;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class ImplServiceSortDetail extends ServiceImpl<MapperSortDetail, DtoSortDetail> implements ServiceSortDetail {
    @Resource
    private ServiceIncoming serviceIncoming;

    @Resource
    private ServiceIncomingDetail serviceIncomingDetail;

    @Resource
    private ServiceSort serviceSort;

    /**
     * 参数查询条件
     */
    @Override
    public LambdaQueryWrapper<DtoSortDetail> fillParams(SearchReqSortDetail params) {
        return null;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public void deleteById(Integer sortId, Integer id) {
        final LambdaQueryWrapper<DtoSortDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtoSortDetail::getSortId, sortId)
                .eq(DtoSortDetail::getId, id);
        final DtoSortDetail sortDetail = getOne(wrapper);
        if (ObjectUtils.isEmpty(sortDetail)) {
            GraceException.display("分拣明细不存在");
        }
        // 清除来料单明细关联信息
        serviceIncomingDetail.deleteRelevancyById(sortDetail.getIncomingId(), sortDetail.getIncomingDetailId());
        // 重新设置来料单关联信息
        serviceSort.setCarNo(sortId, serviceIncomingDetail.listByRelevancyId(sortId,
                EnumWorkType.SORT.getCode()));
        // 删除分拣明细
        this.removeById(id);
    }

    /**
     * 保存分拣明细
     */
    @Override
    @Transactional
    public void saveSortDetail(Integer sortId, Integer incomingId, ReqPublicIds<Integer> incomingDetailIds) {
        // 获取来料单
        DtoIncoming incoming = serviceIncoming.getById(incomingId);
        if (ObjectUtils.isEmpty(incoming)) {
            GraceException.display("来料单不存在");
        }
        // 获取来料单明细
        List<DtoIncomingDetail> detail = serviceIncomingDetail.findByIncomingId(incomingId);
        // 把明细按照id设置为map集合
        Map<Integer, DtoIncomingDetail> detailMap = detail.stream().collect(Collectors.toMap(DtoIncomingDetail::getId
                , v -> v));

        ArrayList<DtoSortDetail> sortDetails = new ArrayList<>();

        // 获取当前需要添加的来料单明细
        for (Integer incomingDetailId : incomingDetailIds.getIds()) {
            DtoIncomingDetail dtoIncomingDetail = detailMap.get(incomingDetailId);
            if (ObjectUtils.isEmpty(dtoIncomingDetail)) {
                GraceException.display("来料单明细【" + incomingDetailId + "】已关联或不存在");
            }
            DtoSortDetail sortDetail = DtoSortDetail.builder()
                    .sortId(sortId)
                    .incomingId(incomingId)
                    .incomingDetailId(dtoIncomingDetail.getId())
                    .materalWeight(dtoIncomingDetail.getGrossWeight())
                    .materialId(dtoIncomingDetail.getMaterialId())
                    .materialName(dtoIncomingDetail.getMaterialInfo())
                    .build();
            sortDetails.add(sortDetail);
        }
        // 保存分拣明细
        this.saveBatch(sortDetails);

        //设置来料单关联明细
        List<ReqRelevancyById> relevancyByIds = sortDetails.stream().map(v -> {
            ReqRelevancyById reqRelevancyById = new ReqRelevancyById();
            reqRelevancyById.setId(v.getIncomingDetailId());
            // 关联对应的分拣工单id
            reqRelevancyById.setRelevancyId(sortId);
            return reqRelevancyById;
        }).toList();
        // 设置来料单关联明细
        serviceIncomingDetail.setRelevancy(relevancyByIds, incomingId, EnumWorkType.SORT.getCode());

        serviceSort.setCarNo(sortId, serviceIncomingDetail.listByRelevancyId(sortId,
                EnumWorkType.SORT.getCode()));

    }
}